package com.yupi.springbootinit.config;

import com.qcloud.cos.COSClient;
import com.qcloud.cos.ClientConfig;
import com.qcloud.cos.auth.BasicCOSCredentials;
import com.qcloud.cos.auth.COSCredentials;
import com.qcloud.cos.region.Region;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 腾讯云对象存储客户端
 * @author 28993
 * @author 28993
 */
@Configuration
/**
 * 这个注解能够读取application.yml的配置资源然后注入到这个配置类的成员变量中
 * @ConfigurationProperties注解详解
 * https://blog.csdn.net/skh2015java/article/details/120141409?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522169547819616800192278072%2522%252C%2522scm%2522%253A%252220140713.130102334..%2522%257D&request_id=169547819616800192278072&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~top_click~default-1-120141409-null-null.142^v94^insert_down28v1&utm_term=%40ConfigurationProperties%E6%B3%A8%E8%A7%A3&spm=1018.2226.3001.4187
 */
@ConfigurationProperties(prefix = "cos.client")
@Data
public class CosClientConfig {

    /**
     * accessKey
     */
    private String accessKey;

    /**
     * secretKey
     */
    private String secretKey;

    /**
     * 区域
     */
    private String region;

    /**
     * 桶名
     */
    private String bucket;

    @Bean
    public COSClient cosClient() {
        // 初始化用户身份信息(secretId, secretKey)
        COSCredentials cred = new BasicCOSCredentials(accessKey, secretKey);
        // 设置bucket的区域, COS地域的简称请参照 https://www.qcloud.com/document/product/436/6224
        ClientConfig clientConfig = new ClientConfig(new Region(region));
        // 生成cos客户端
        return new COSClient(cred, clientConfig);
    }
}